package components;

import javax.swing.DefaultListModel;
import javax.swing.JList;

@SuppressWarnings("serial")
public class JListaStrings extends JList<String> {

	DefaultListModel<String> model;

	public JListaStrings(){
		model = new DefaultListModel<String>();
		this.setModel(model);		
	}
	
	public void addItem(String item){
		model.addElement(item);
	}

	public void addItems(String[] items){
		for (String item : items) {
			model.addElement(item);	
		}
	}
	
	public void remover(int index){
		model.remove(index);
	}
	
	public void clear(){
		model.clear();
	}
	
	public String getItem(int index){
		return model.get(index);
	}

	public void setItemIndex(String item, int index){
		model.setElementAt(item, index);
	}
	
	public String[] getItems() {
		String[] items = new String[model.size()];
		for (int i = 0; i < model.size(); i++) {
			items[i] = model.get(i);
		}
		return items;
	}
/*
	public int getItemIndex(String jogAtivo) {
		int i = 0;
		for(i = 0; i < model.size(); i++)
			if(getItem(i).equals(jogAtivo))
				break;
		return i-1;
	}
	*/
	public int getItemIndexJogador(String jogAtivo) {
		int i = 0;
		for(i = 0; i < model.size(); i++){
			String item[] = getItem(i).split(" - ");
			if(item[0].equals(jogAtivo))
				break;
		}
		return i;
	}

	public int getTamanho(){
		return model.size();
	}

	public boolean contains(String servidor) {
		boolean res = false;
		for (int i = 0; i < model.size(); i++)
			if(model.get(i).equals(servidor)){
				res = true;
				break;
			}
		return res;
	}

}
